var webpack = require('webpack');
var Clean = require('clean-webpack-plugin');

var CommonsChunkPlugin = webpack.optimize.CommonsChunkPlugin;

var isProduction = process.env.NODE_ENV === 'production'

function getEntrySources(sources) {
    if (!isProduction) {
        sources.unshift('webpack/hot/only-dev-server');
        sources.unshift('webpack-dev-server/client?http://localhost:8080');
    }
    return sources;
}

var jsLoaders = ['babel'];

if (!isProduction) {
    jsLoaders.unshift('react-hot');
}

module.exports = {
    entry: {
        main: getEntrySources([
            './js/main'
        ]),
        vendor: ['react']
    },
    output: {
        path: './dest/',
        filename: '[name].js',
        chunkFilename: "[id].js",
        publicPath: '/dest/',
        jsonpFunction: '__wpj_'
    },
    module: {
        loaders: [{
            test: /\.js[x]?$/,
            exclude: /node_modules/,
            loaders: jsLoaders
        }]
    },
    plugins: [
        new Clean(['dest']),
        new webpack.optimize.OccurenceOrderPlugin(),
        new CommonsChunkPlugin('vendor', 'vendor.js')
    ]
};